/*
 * VHMainView.java
 * Created on 28 mai 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.hockeo.vhweb.client.mvc.VHView;
import com.hockeo.vhweb.client.res.VHResConstants;
import com.hockeo.vhweb.client.ui.widget.VHNavCountryTree;
import com.hockeo.vhweb.client.ui.widget.VHNavMainTree;

/**
 * Main view of the VH application. Serves as a template.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHMainView extends VHView
{
  private Viewport         d_viewport;
  private ContentPanel     d_pnWest;
  private ContentPanel     d_pnMain;
  private VHNavMainTree    d_navTreeMain;
  private VHNavCountryTree d_navTreeCountries;

  /*
   * (non-Javadoc)
   *
   * @see com.hockeo.vhweb.client.mvc.VHView#init()
   */
  @Override
  public void init()
  {
    d_viewport = new Viewport();
    d_viewport.setLayout( new BorderLayout() );

    createNorth();
    createWest();
    createCenter();

    // Get rid of scrollbars, and clear out the window's built-in margin,
    // because we want to take advantage of the entire client area.
    //
    Window.enableScrolling( false );
    Window.setMargin( "0px" );

    RootPanel.get().add( d_viewport );
  }

  private void createNorth()
  {
    StringBuffer sb = new StringBuffer();
    sb.append( "<div id='page-header' class='x-small-editor'><div id='page-title'>VirtualHockey v" );
    sb.append( VHMain.getAppConstants().getAppVersion() );
    sb.append( "</div></div>" );

    HtmlContainer htmlNorth = new HtmlContainer( sb.toString() );

    BorderLayoutData data = new BorderLayoutData( LayoutRegion.NORTH, 33 );
    d_viewport.add( htmlNorth, data );
  }

  private void createWest()
  {
    d_pnWest = new ContentPanel();
    d_pnWest.setLayout( new AccordionLayout() );
    d_pnWest.setHeading( VHMain.getConstants().navHeader() );

    ToolBar toolBar = new ToolBar();
    d_pnWest.setTopComponent( toolBar );

    createNavPanels( d_pnWest );

    BorderLayoutData data = new BorderLayoutData( LayoutRegion.WEST, 270, 180, 320 );
    data.setMargins( new Margins( 5, 5, 5, 5 ) );
    data.setCollapsible( true );
    d_viewport.add( d_pnWest, data );
  }

  private void createCenter()
  {
    d_pnMain = new ContentPanel();
    d_pnMain.setLayout( new FitLayout() );

    BorderLayoutData data = new BorderLayoutData( LayoutRegion.CENTER );
    data.setMargins( new Margins( 5, 5, 5, 0 ) );
    data.setCollapsible( false );
    d_viewport.add( d_pnMain, data );
  }

  /**
   * Sets the content of the main window area.
   *
   * @param widget the widget to appear in the main window area
   * @param strTitle
   * @param icon
   */
  public void setMainContent( Widget widget, String strTitle, AbstractImagePrototype icon )
  {
    d_pnMain.removeAll();
    d_pnMain.setHeading( strTitle );
    d_pnMain.setIcon( icon );
    d_pnMain.add( widget );
    d_pnMain.layout();
  }

  private void createNavPanels( ContentPanel parent )
  {
    final VHView view = this;
    VHResConstants c = VHMain.getConstants();

    // main menu (shown by default)
    //
    d_navTreeMain = new VHNavMainTree( view );
    final ContentPanel pnMainMenu = new ContentPanel();
    pnMainMenu.setHeading( c.navCategoryMain() );
    pnMainMenu.setBorders( false );
    parent.add( pnMainMenu );

    pnMainMenu.setLayout( new FitLayout() );
    pnMainMenu.setLayoutOnChange( true );
    pnMainMenu.setScrollMode( Scroll.AUTOY );
    pnMainMenu.add( d_navTreeMain );

    // countries tree
    //
    final ContentPanel pnCountryMenu = new ContentPanel();
    pnCountryMenu.setHeading( c.navCategoryCountries() );
    pnCountryMenu.setBorders( false );
    parent.add( pnCountryMenu );

    pnCountryMenu.setLayout( new FitLayout() );
    pnCountryMenu.setLayoutOnChange( true );
    pnCountryMenu.setScrollMode( Scroll.AUTOY );
    pnCountryMenu.addListener( Events.BeforeExpand, new Listener<ComponentEvent>() {
      @SuppressWarnings( "synthetic-access" )
      public void handleEvent( ComponentEvent evt )
      {
        if ( d_navTreeCountries == null )
        {
          d_navTreeCountries = new VHNavCountryTree( view );
          pnCountryMenu.add( d_navTreeCountries );
        }
      }
    } );
  }
}
